$('.login-btn').on('click', function () {
  const username = $('#username').val();
  const password = $('#password').val();

  if (!username) {
    return alert('请输入用户名');
  }
  if (!password) {
    return alert('请输入密码');
  }

  $.ajax({
    url: '/users/login',
    method: 'POST',
    data: {
      username,
      password: MD5(password)
    }
  })
    .done(res => {
      if (res.code == '200') {
        localStorage.setItem('userid', res.data.userId);
        localStorage.setItem('username', username);
        localStorage.setItem('avator', res.data.avator);

        location.href = '/views/chat.html';
      } else {
        alert(res.msg);
        $('#username').val('');
        $('#password').val('');
      }
    })
    .fail(err => {
      console.log('请求失败：', err);
    });
});